import router from '../router'
import ElementUI from 'element-ui'

// 全局函数
export default {

  install(Vue, options) {
    /**
    *  储存localStorage
    */
    Vue.prototype.setlocalStorage = (storeName, storeData) => {
      var storeStr = JSON.stringify(storeData);
      var storeArr = window.localStorage.setItem(storeName, storeStr);
      return storeArr;
    },
    /**
    *  获取localStorage
    */
    Vue.prototype.getlocalStorage = (storeName) => {
      var storeName = localStorage.getItem(storeName);
    	var storeArr = JSON.parse(storeName)
    	return storeArr;
    },

    /**
    *  获取url后面的参数
    */
    Vue.prototype.getUrlParams = (name) => {
      var _reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"),
          _regNext = window.location.search.substr(1).match(_reg);
      if (_regNext != null) return decodeURI(_regNext[2]) || '';
      else return '';
    },

    /**
    *  tags逗号隔开
    */
    Vue.prototype.splitTags = (str) => {
      if(str){
        if(str.indexOf(',')){
          var strs = str.split(",");
          return strs
        }else{
          return str
        }
      }
    },

    /**
    *  跳转
    */
    Vue.prototype.routeToPath = (url) => {
      return url?router.push(url):router.go(-1)

    },

    /**
    *  打印
    */
    Vue.prototype.log = (obj, type) => {
      type = type || "log";
      const log = JSON.parse(JSON.stringify(obj));
      return console[type](log)
    },

    /**
    *  操作成功之后
    */
    Vue.prototype.actionsAfter = (tips, type, url) => {
      ElementUI.Message({
        type: type,
        message: '提示：'+tips
      });
      setTimeout(()=>{
        return url?router.go(-1):''
      }, 1000)
    },
    /**
    *  格式化时间
    */
    Vue.prototype.fmtDate = (date, type) => {
      if(date == null){
        return null
      }
      function add0(m) {
          return m < 10 ? '0' + m: m
      }
      var time = new Date(date);
      var y = time.getFullYear();
      var m = time.getMonth() + 1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();
      if(type && type == 'ymd'){
        return y + '-' + add0(m) + '-' + add0(d)
      }else if(type && type == 'ymd2'){
        return y + '年' + add0(m) + '月' + add0(d) + '日'
      }else if(type && type == 'ymd3'){
        return add0(m) + '-' + add0(d) + ' '+ add0(h) + ':' + add0(mm)
      }else{
        return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
      }
    },
    /**
    *  yy年mm月dd日 转 yy-mm-dd
    */
    Vue.prototype.fmtDateType = (str) => {
      str = str.replace(/[^\d]/g,'/');
      var date = new Date(str);
      return date
    },
    /**
    *  刷新当前页面
    */
    Vue.prototype.reloadPage = (time) => {
      setTimeout(() => {
        router.go(0)
      }, time)
    }
  }
}
